package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.json.JSONArray;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.operate.trading.api.parameter.ApplyForPostPayBean;
import com.ymatou.operate.trading.api.parameter.PlaceOrderBean;
import com.ymatou.operate.trading.api.service.ApplyForPostPayCall;
import com.ymatou.operate.trading.api.service.PlaceOrderCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.business.service.ProductCallService;
import com.ymttest.common.entitie.OrderInfo;
import com.ymttest.common.entitie.OrderProductInfo;
import com.ymttest.common.entitie.ProductActivityInfo;
import com.ymttest.common.enums.ActivityPromotionTypeEnum;
import com.ymttest.common.enums.AppTerminalSourceEnum;
import com.ymttest.common.enums.BondedAreaEnum;
import com.ymttest.common.enums.LogisticsTypeEnum;
import com.ymttest.common.enums.OrderSourceEnum;
import com.ymttest.common.enums.OrderTypeEnum;
import com.ymttest.common.enums.PriceTypeEnum;
import com.ymttest.common.enums.ProductRefundChannelEnum;
import com.ymttest.common.enums.StockStatusEnum;
import com.ymttest.common.enums.TerminalSourceEnum;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 卖家申请补款 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ApplyForPostPay {
    private static ApplyForPostPayBean afppbean;
    private static ApplyForPostPayCall afppcall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("卖家申请补款");
    }

    @Before
    public void caseUp() {

        afppbean = new ApplyForPostPayBean();
        afppcall = new ApplyForPostPayCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(afppcall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    @Ignore
    public void Tc_ApplyForPostPay_001() {
        Logger.start(true, "卖家申请补款成功");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            Map map = new OrderCallService().appSaveOrder();
            int orderId = (Integer) map.get("orderId");
            int sellerId = (Integer) map.get("sellerId");
            int userId = (Integer) map.get("userId");
            int tradingId = (Integer) map.get("tradingId");
            DecimalFormat df = new DecimalFormat("0.00");// 保留两位小数
            BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 金额
            /** 买家付定金 **/
            new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);
            // 接单
            new OrderCallService().acceptOrder(orderId, sellerId);
            Logger.comment("前置数据准备结束");
            afppbean.setApplyReason("自动化测试");
            afppbean.setOrderId(orderId);
            afppbean.setPostPayType(1);
            BigDecimal OriginalPrice = new BigDecimal(121);
            afppbean.setPostPayAmount(OriginalPrice);
            afppbean.setSellerId(sellerId);
            // 发送请求
            afppcall.setData(afppbean);
            afppcall.callService();
            Logger.verifyEquals(ECode.SUCCESS, afppcall.getCode(), "验证Code");
            Logger.verifyNotNull(afppcall.getPostPayId(), "验证PostPayId");
            // 数据库验证
            DefaultWapper database = new DefaultWapper();
            HashMap m = new HashMap();
            m.put("OrderId", orderId);
            m.put("TraddingId", orderId);
            m.put("TradingStatus", 16);
            m.put("SellerId", sellerId);
            Thread.sleep(1000);
            Logger.verifyEquals(1, database.selectOrder(m).size(), "验证订单表记录");
            Logger.verifyEquals(1, database.selectOrderPostPayByOrderId(orderId).size(), "验证补款记录表");
            Logger.verifyEquals(1, database.selectTradingStatus(m).size(), "验证交易表记录");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P3.class)
    @TestCase
    public void Tc_ApplyForPostPay_002() {
        Logger.start(true, "订单不存在，卖家申请补款失败");
        try {
            // 参数
            int OrderId = 541154;
            int SellerId = 288484;
            afppbean.setApplyReason("自动化测试");
            afppbean.setOrderId(OrderId);
            afppbean.setPostPayType(1);
            BigDecimal OriginalPrice = new BigDecimal(121);
            afppbean.setPostPayAmount(OriginalPrice);
            afppbean.setSellerId(SellerId);
            // 发送请求
            afppcall.setData(afppbean);
            afppcall.callService();
            Logger.verifyEquals(ECode.FAILED201, afppcall.getCode(), "验证Code");
            Logger.verifyEquals("订单号" + OrderId + "不存在", afppcall.getMsg(), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P3.class)
    @TestCase
    public void Tc_ApplyForPostPay_003() {
        Logger.start(true, "当前状态不能申请补款");
        try {
            // 创建订单
            Logger.comment("前置数据准备");
            Map map = new OrderCallService().appSaveOrder();
            int orderId = (Integer) map.get("orderId");
            int sellerId = (Integer) map.get("sellerId");
            int userId = (Integer) map.get("userId");
            int tradingId = (Integer) map.get("tradingId");
            DecimalFormat df = new DecimalFormat("0.00");// 保留两位小数
            BigDecimal totalPrice = (BigDecimal) map.get("totalPrice");// 支付金额
            // 接单
            new OrderCallService().acceptOrder(orderId, sellerId);
            Logger.comment("前置数据准备结束");
            afppbean.setApplyReason("自动化测试");
            afppbean.setOrderId(orderId);
            afppbean.setPostPayType(1);
            BigDecimal OriginalPrice = new BigDecimal(121);
            afppbean.setPostPayAmount(OriginalPrice);
            afppbean.setSellerId(sellerId);
            // 发送请求
            afppcall.setData(afppbean);
            afppcall.callService();
            Logger.verifyEquals(ECode.FAILED201, afppcall.getCode(), "验证Code");
            Logger.verifyEquals(true, afppcall.getMsg().contains("当前状态不能申请补款"), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

}
